package org.xqp.pet.service.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xqp.basic.exception.MyException;
import org.xqp.basic.service.impl.BaseServiceImpl;
import org.xqp.pet.domain.Pet;
import org.xqp.pet.domain.PetDetail;
import org.xqp.pet.mapper.PetDetailMapper;
import org.xqp.pet.mapper.PetMapper;
import org.xqp.pet.service.IPetService;

import java.util.Date;
import java.util.List;

@Service
public class PetServiceImpl  extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;

    @Override
    public void insert(Pet pet) {
        // 将pet数据存入表中 并返回自增主键
        petMapper.add(pet);
        Long petId = pet.getId();
        // 再将petDetail存入表中
        PetDetail detail = pet.getDetail(); // 通过pet获取到PetDetail
        detail.setPet_id(petId); // 将pet的id传给PetDetail
        petDetailMapper.add(detail); // 存入数据库
    }

    /**
     * 宠物上架
     * 1.参数判断，ID不能为空
     * 2.宠物状态必须是下架才能上架
     * 3.修改宠物的状态为“上架”
     * @param ids ：宠物的ID集合
     */
    @Override
    public void publish(List<Long> ids) {
        //* 1.参数判断，ID不能为空
        if(ids == null || ids.size() == 0){
            throw new MyException("非法请求");
        }
        //* 2.宠物状态必须是下架才能上架
        //老师没写有时间自己完成

        //批量查询状态是上架的宠物的宠物列表
        List<Pet> pets = petMapper.batchSelectByIds(ids,Pet.STATE_OFFLINE);
        if(pets==null){
            throw  new MyException("已上架请勿重复上架");
        }
        Date now = new Date();
        pets.forEach(pet -> { // 将查询出来的pet对象遍历修改状态为下架并设置时间
            //* 3.修改宠物的状态为“上架”
            //设置宠物状态为上架
            pet.setState(Pet.STATE_ONLINE);
            pet.setOnsaletime(now);
            petMapper.update(pet);
        });
    }

    /**
     * 宠物下架
     * 1.参数判断，ID不能为空
     * 2.宠物状态必须是上架才能上架
     * 3.修改宠物的状态为“下架”
     * @param ids ：宠物的ID集合
     */
    @Override
    public void offsale(List<Long> ids) {
        //* 1.参数判断，ID不能为空
        if(ids == null || ids.size() == 0){
            throw new MyException("非法请求");
        }
        //* 2.宠物状态必须是下架才能上架
        //update pet set offsaletime = now() ,state = #{state} where id in ()

        //update   pet set online = now() , state = #{state} where id in ()

        //petMapper.updateSate（"Onsaletime",）
        //petMapper.updateSate(String timeField, int state,List<Long>ids);
        //update pet set ${timeField} = now() ,state = #{state} where id in (1,2,3)

        //批量查询状态是上架的宠物的宠物列表
        List<Pet> pets = petMapper.batchSelectByIds(ids,Pet.STATE_ONLINE);
        if(pets==null){
            throw  new MyException("已下架请勿重复下架");
        }
        Date now = new Date();
        pets.forEach(pet -> {
            //* 3.修改宠物的状态为“上架”
            //设置宠物状态为上架
            pet.setState(Pet.STATE_OFFLINE);
            pet.setOffsaletime(now);
            petMapper.update(pet);
        });
    }
    // 查询上线的宠物以及宠物和宠物详情
    @Override
    public Pet selectOnLineById(Long id) {
        Pet pet = petMapper.selectByIdAndState(id, Pet.STATE_ONLINE);
        return pet;
    }

//    @Override
//    public Pet selectOnLineById(Long id) {
//        return petMapper.selectByIdAndState(id,Pet.STATE_ONLINE);
//    }
}
